package com.codingke.codingbookstore.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.adapter.ShoppingCartListItemAdapter;
import com.codingke.codingbookstore.adapter.ShoppingCartListItemEditAdapter;
import com.codingke.codingbookstore.utils.Constant;
import com.codingke.codingbookstore.utils.NumberUtils;
import com.codingke.codingbookstore.vo.Orders;
import com.codingke.codingbookstore.vo.User;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

public class ShoppingCartActivity extends BaseActivity implements View.OnClickListener{

    private Button button_Shopping_Cart_edit,button_Shopping_Cart_finish,button_Settlement;
    private ListView listView2_shopping_cart;
    private ShoppingCartListItemAdapter shoppingCartListItemAdapter;
    private ShoppingCartListItemEditAdapter shoppingCartListItemEditAdapter;
    private TextView textView5_count,textView4_sum;
    private ArrayList<Orders> orderList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_cart);
        button_Shopping_Cart_edit = (Button) findViewById(R.id.button_Shopping_Cart_edit);
        button_Shopping_Cart_finish = (Button) findViewById(R.id.button_Shopping_Cart_finish);
        button_Shopping_Cart_edit.setOnClickListener(this);
        button_Shopping_Cart_finish.setOnClickListener(this);
        listView2_shopping_cart = (ListView) findViewById(R.id.listView2_shopping_cart);
        textView5_count = (TextView) findViewById(R.id.textView5_count);
        textView4_sum = (TextView) findViewById(R.id.textView4_sum);
        button_Settlement = (Button) findViewById(R.id.button_Settlement);
        button_Settlement.setOnClickListener(this);
        button_Settlement.setEnabled(false);

        shoppingCartListItemAdapter = new ShoppingCartListItemAdapter(this,orderList);
        shoppingCartListItemEditAdapter = new ShoppingCartListItemEditAdapter(this,orderList);
        listView2_shopping_cart.setAdapter(shoppingCartListItemAdapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();//初始化购物车
    }

    //设置总金额到UI组件
    public void setTotalAmountToUI(){
        double totalAmount = getTotalAmount();
        textView4_sum.setText("金额:"+ NumberUtils.format(totalAmount)+"元");
        if (totalAmount<=0){
            button_Settlement.setEnabled(false);
        }else{
            button_Settlement.setEnabled(true);
        }
    }

    //设置叫商品数
    public void setProductCount(){
        textView5_count.setText("共"+orderList.size()+"件商品");
    }

    //计算总金额
    private double getTotalAmount() {
        double totalAmount = 0;
        Orders orders;
        for (int i=0;i<orderList.size();i++){
            orders = orderList.get(i);
            totalAmount+=orders.getSubtotal();
        }
        return totalAmount;
    }

    //初始化查询购物车
    private void initData(){
        BmobQuery<Orders> query = new BmobQuery<>();
        User user = BmobUser.getCurrentUser(this,User.class);
        query.addWhereEqualTo("userId",user.getObjectId());
        query.addWhereEqualTo("status", Constant.ORDER_NON_PAYMENT);

        query.findObjects(this, new FindListener<Orders>() {
            @Override
            public void onSuccess(List<Orders> list) {
//                if(list.size()>0) {
                    orderList = (ArrayList<Orders>) list;
                    shoppingCartListItemAdapter.setOrderList(orderList);
                    shoppingCartListItemAdapter.notifyDataSetChanged();
                    setProductCount();
                    setTotalAmountToUI();
//                }

            }
            @Override
            public void onError(int i, String s) {
                Log.e(getClass().getName(),s);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_Shopping_Cart_edit:
                button_Shopping_Cart_edit.setVisibility(View.GONE);
                button_Shopping_Cart_finish.setVisibility(View.VISIBLE);
                shoppingCartListItemEditAdapter.setOrderList(orderList);
                listView2_shopping_cart.setAdapter(shoppingCartListItemEditAdapter);
                //shoppingCartListItemEditAdapter.notifyDataSetChanged();


                break;
            case R.id.button_Shopping_Cart_finish:
                button_Shopping_Cart_edit.setVisibility(View.VISIBLE);
                button_Shopping_Cart_finish.setVisibility(View.GONE);
                listView2_shopping_cart.setAdapter(shoppingCartListItemAdapter);
                break;

            case R.id.button_Settlement:
                //去结算
                Intent intent = new Intent(this,SettlementActivity.class);
                intent.putExtra("totalAmount",getTotalAmount());
                intent.putExtra("orderList",orderList);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

//    //监听总金额变化的接口
//    public interface CalculationTotalAmountListener{
//        public void setCalculationTotalAmount(TextView view);
//    }
}
